/*
 * 文件名： LoginActivity.java
 * 描    述：登录页面
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */
package com.miguo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

import com.miguo.activity.auth.AuthActivity;
import com.miguo.activity.fragment.LoginFragment;
import com.miguo.app.AppContext;
import com.miguo.bean.AuthLoginBean;
import com.miguo.bean.UserBean;
import com.miguo.component.BaseHeadView;
import com.miguo.framework.request.JsonData;
import com.miguo.framework.uri.UriError;
import com.miguo.framework.uri.UriException;
import com.miguo.framework.uri.UriListener;
import com.miguo.framework.utils.FragmentUtils;
import com.miguo.framework.utils.Toast;
import com.miguo.http.HttpClient;
import com.miguo.http.Url;
import com.miguo.http.callback.AjaxCallBack;
import com.miguo.ui.R;

import java.util.HashMap;
import java.util.Map;


public class LoginActivity extends AuthActivity {

    public String mUserName;
    public String mPassword;

    public BaseHeadView mBaseHead;

    private AjaxCallBack callBack;

    private FragmentManager mFragmentManager;

    private Fragment mCurrentFragment;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_attach);
        if(AppContext.UserInfo().getIsLogin() == true){
            Toast.show(mContext, "当前已登录");
            this.onBackPressed();
        }
        mFragmentManager = getSupportFragmentManager();
        initTitle();
        initView();
    }

    private void initTitle() {
        mBaseHead = (BaseHeadView) findViewById(R.id.base_head);
        mBaseHead.setActivity(this);
    }

    private void initView() {
        mCurrentFragment = FragmentUtils.replaceFragment(mFragmentManager, R.id.fragment_center, LoginFragment.class);
    }

    public void replaceFragment(Class<? extends Fragment> newFragment) {
        mCurrentFragment = FragmentUtils.switchFragment(mFragmentManager,
                R.id.fragment_center, mCurrentFragment,
                newFragment, null, false, true, true);
    }

    public void login() {
        initCallBack();
        Map<String, String> param = new HashMap<String, String>();
        param.put("username", mUserName);
        param.put("password", mPassword);
        HttpClient.postEncryptData(Url.Passport.User.Login, param, callBack);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        parserUri();
    }

    @Override
    protected UriListener<AuthLoginBean> getAuthorizeListener() {
        return new UriListener<AuthLoginBean>() {

            @Override
            public void onComplete(AuthLoginBean AuthLogin) {
                initCallBack();
                AppContext.UserInfo().setUserToken(AuthLogin);
                Map<String, String> param = new HashMap<String, String>();
                param.put("token", AuthLogin.getToken());
                HttpClient.postEncryptData(Url.toRoute(Url.UserCentre.UserInfo, 1),
                        param, callBack);
            }

            @Override
            public void onError(UriError e) {
                Toast.show(mContext, e.getErrorDescription());
            }

            @Override
            public void onException(UriException e) {
                Toast.show(mContext, e.getMessage());
            }
        };
    }

    private void initCallBack() {
        callBack = new AjaxCallBack(mActivity, "正在登录...") {
            @Override
            public void onRequestFinish(JsonData json) {
                super.onRequestFinish(json);
                if (json.optInt("status") == 200) {
                    JsonData data = json.optJson("data");
                    AppContext.UserInfo().setUserInfo(data);
                    setResult(RESULT_OK);
                    LoginActivity.this.finish();
                } else {
                    Toast.show(mContext, json.optString("info"));
                }
            }
        };
    }
}
